#include <iostream>
#include <cstring>
#include <arpa/inet.h>
#include <unistd.h>

using namespace std;

int main() {
    const char *ipAddress = "192.168.138.156";
    const int port = 50000;
    const char *message = "Hello world";

    // 创建套接字
    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_fd < 0) {
        cerr << "Socket creation failed." << endl;
        return -1;
    }

    // 设置服务器地址
    sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(port);
    inet_pton(AF_INET, ipAddress, &serverAddr.sin_addr);

    // 连接到服务器
    if (connect(socket_fd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
        cerr << "Connection failed." << endl;
        close(socket_fd);
        return -1;
    }

    // 发送消息
    send(socket_fd, message, strlen(message), 0);

    cout << "Message sent: " << message << endl;

    sleep(10);
    // 关闭套接字
    close(socket_fd);
    return 0;
}
